﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.ComponentModel;
using xIx.Web.AccessControl;


[ParseChildren(true,"LoginTemplate")]
[PersistChildren(true)]
[PersistenceMode( PersistenceMode.InnerProperty)]
[Designer(typeof(System.Web.UI.Design.ControlDesigner))]
public partial class UserControls_LoginPan : xIx.Web.UI.ControlBase
{

    public ITemplate _LoginTemplate;
    
    [PersistenceMode(PersistenceMode.InnerProperty), TemplateContainer(typeof(TemplateControl)), TemplateInstance(TemplateInstance.Single)]
    [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
    public ITemplate LoginTemplate{get{return _LoginTemplate;}set{_LoginTemplate=value;}}

    public ITemplate _UserInfoTemplate;

    [PersistenceMode(PersistenceMode.InnerProperty), TemplateContainer(typeof(TemplateControl)), TemplateInstance(TemplateInstance.Single)]
    [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]

    public ITemplate UserInfoTemplate{get{return _UserInfoTemplate;}set{_UserInfoTemplate=value;}}

    public ITextControl UserNameCtl, UserPwdCtl;
    public IButtonControl LoginBtn, LogoffBtn;

    protected void Page_Load(object sender, EventArgs e)
    {
        MultiView1.ActiveViewIndex = (Page.User != null && Page.User.Identity.IsAuthenticated) ? 0 : 1;
        if (MultiView1.ActiveViewIndex == 0)
        {
            if (null != (FindControl("UserTitle") as ITextControl))
            {
                (FindControl("UserTitle") as ITextControl).Text = Page.User.AccountInfo.Title;
            }
        }
    }

    public bool _DoRedirect;
    public bool DoRedirect { get { return _DoRedirect; } set { _DoRedirect = value; } }
	
    protected override void OnInit(EventArgs e)
    {
        base.OnInit(e);
        if (null != LoginTemplate) LoginTemplate.InstantiateIn(View2.FindControl("PlhLi"));
        if (null != UserInfoTemplate) UserInfoTemplate.InstantiateIn(View1.FindControl("PlhLo"));
        UserNameCtl = FindControl("UserName") as ITextControl;
        UserPwdCtl = FindControl("Password") as ITextControl;

    }

    protected void OnCommand(IButtonControl b,CommandEventArgs e) {
        if ("Login".Equals(b.CommandName, StringComparison.CurrentCultureIgnoreCase))
        {
            LoginButton_Click(b, e);  
        }else if ("Logoff".Equals(b.CommandName, StringComparison.CurrentCultureIgnoreCase)) {
            LogoffButton_Click(b, e);
        }
    }
    
    protected override bool OnBubbleEvent(object source, EventArgs args)
    {
        IButtonControl bc = source as IButtonControl;
        if (null != bc && !string.IsNullOrEmpty(bc.CommandName))
        {
            OnCommand(bc, args as CommandEventArgs);
            return true;
        }
        else
        {
            return base.OnBubbleEvent(source, args);
        }
    }

    protected void LogoffButton_Click(object sender, EventArgs e)
    {
        FormsAuthentication.SignOut();
        if (DoRedirect)
        {
            Response.Redirect(FormsAuthentication.LoginUrl);
        }
        else
        {
            Response.Redirect(Request.RawUrl);
        }
    }

    public string _pwderrorstr;
    public string pwderrorstr { get { return _pwderrorstr; } set { _pwderrorstr = value; } }
	
    protected void LoginButton_Click(object sender, EventArgs e)
    {
        Page.Validate("Login");
        if (!Page.IsValid) return;

        string u = UserNameCtl.Text;
        string p = UserPwdCtl.Text;

        BizAccountInfo ai = BizPrincipal.TryLogin(u, p);
        if (null == ai) { Alert("登录错误。\\r\\nLogin error."); return; }

        BizPrincipal.SetAuthCookie(ai, true);
        if (DoRedirect)
        {
            Response.Redirect(FormsAuthentication.DefaultUrl);
        }else{
            Response.Redirect(Request.RawUrl);
        }
    }

}
